﻿#include "DeviceInfo.h"
#include "../util/JsonUtil.h"

DeviceInfo::DeviceInfo()
	: m_enabled(1)
	, m_frameGap(25)
	, m_silence(5)
{
}

DeviceInfo::~DeviceInfo()
{
}

std::string DeviceInfo::GetId()
{
	return m_id;
}

void DeviceInfo::SetId(std::string id)
{
	m_id = id;
}

std::string DeviceInfo::GetName()
{
	return m_name;
}

void DeviceInfo::SetName(std::string name)
{
	m_name = name;
}

std::string DeviceInfo::GetRtspUrl()
{
	return m_rtspUrl;
}

void DeviceInfo::SetRtspUrl(std::string rtspUrl)
{
	m_rtspUrl = rtspUrl;
}

std::string DeviceInfo::GetUsername()
{
	return m_username;
}

void DeviceInfo::SetUsername(std::string username)
{
	m_username = username;
}

std::string DeviceInfo::GetPassword()
{
	return m_password;
}

void DeviceInfo::SetPassword(std::string password)
{
	m_password = password;
}

std::string DeviceInfo::GetManufacture()
{
	return m_manufacture;
}

void DeviceInfo::SetManufacture(std::string manufacture)
{
	m_manufacture = manufacture;
}

int DeviceInfo::GetEnabled()
{
	return m_enabled;
}

void DeviceInfo::SetEnabled(int enabled)
{
	m_enabled = enabled;
}

int DeviceInfo::GetFrameGap()
{
	return m_frameGap;
}

void DeviceInfo::SetFrameGap(int frameGap)
{
	m_frameGap = frameGap;
}

std::string DeviceInfo::GetStartTime()
{
	return m_startTime;
}

void DeviceInfo::SetStartTime(std::string time)
{
	m_startTime = time;
}

std::string DeviceInfo::GetEndTime()
{
	return m_endTime;
}

void DeviceInfo::SetEndTime(std::string time)
{
	m_endTime = time;
}

std::string DeviceInfo::GetParams()
{
	return m_params;
}

void DeviceInfo::SetParams(std::string params)
{
	m_params = params;
}

std::string DeviceInfo::GetPolygon(int width, int height, std::vector<cv::Point2f>& list_points)
{
	list_points.push_back(cv::Point2f(0, 0));
	list_points.push_back(cv::Point2f(width, 0));
	list_points.push_back(cv::Point2f(width, height));
	list_points.push_back(cv::Point2f(0, height));
	if (m_params.empty())
	{
		return "polygon";
	}

	try
	{
		std::stringstream iss;
		iss.str(m_params.c_str());

		boost::property_tree::ptree m_root;
		boost::property_tree::json_parser::read_json(iss, m_root);
		std::string type = m_root.get<std::string>("type", "");
		boost::property_tree::ptree points = m_root.get_child("points");

		list_points.clear();
		for (BOOST_AUTO(pos, points.begin()); pos != points.end(); ++pos)
		{
			std::string x = pos->second.get<std::string>("x", "0");
			std::string y = pos->second.get<std::string>("y", "0");
			cv::Point2f pt = cv::Point2f(std::stof(x), std::stof(y));
			list_points.push_back(pt);
		}

		return type;
	}
	catch (boost::property_tree::ptree_error& e)
	{
		printf("%s\n", e.what());
	}

	return "polygon";
}

bool DeviceInfo::PointIn(int width, int height, cv::Point2f pt)
{
	std::vector<cv::Point2f> points;
	std::string type = GetPolygon(width, height, points);
	if ("line" == type)
	{
		for (std::vector<cv::Point2f>::iterator it = points.begin(); it != points.end(); ++it)
		{
			cv::Point2f pt2;
			cv::Point2f pt1 = cv::Point2f(it->x, it->y);
			std::vector<cv::Point2f>::iterator last = points.end() - 1;
			if (it == last)
			{
				pt2 = cv::Point2f(points.begin()->x, points.begin()->y);
			}
			else
			{
				std::vector<cv::Point2f>::iterator next = it + 1;
				pt2 = cv::Point2f(next->x, next->y);
			}

			cv::Point2f vec1 = pt - pt1;
			cv::Point2f vec2 = pt2 - pt1;
			cv::Point2f vec3 = pt - pt2;

			// 计算点到线段起点和终点的距离
			double dist1 = cv::norm(vec1);
			double dist2 = cv::norm(vec3);

			// 如果点在线段的右侧，则距离为点到终点的距离
			if ((pt1 - pt).cross(pt2 - pt) >= 0) {
				return abs(std::min<double>(dist1, dist2) - 2) <= 0;
			}

			// 如果点在线段的左侧，则计算垂直平分线距离
			double dist = std::fabs((vec1.cross(vec2)) / cv::norm(vec2));

			// 返回最小距离
			return abs(std::min<double>(std::min<double>(dist1, dist2), dist) - 2) <= 0;
		}
	}
	else
	{
		cv::Mat matPolygon(points);
		double d = cv::pointPolygonTest(matPolygon, pt, false);
		return d > 0;
	}
}

int DeviceInfo::GetSilence()
{
	return m_silence;
}

void DeviceInfo::SetSilence(int silence)
{
	m_silence = silence;
}

DateTime DeviceInfo::GetCreateTime()
{
	return m_createTime;
}

void DeviceInfo::SetCreateTime(DateTime& time)
{
	m_createTime = time;
}

DateTime DeviceInfo::GetUpdateTime()
{
	return m_updateTime;
}

void DeviceInfo::SetUpdateTime(DateTime& time)
{
	m_updateTime = time;
}

DateTime DeviceInfo::GetDetectTime()
{
	return m_detectTime;
}

void DeviceInfo::SetDetectTime(DateTime time)
{
	m_detectTime = time;
}

std::string DeviceInfo::GetLastImage()
{
	return m_lastImage;
}

void DeviceInfo::SetLastImage(std::string img)
{
	m_lastImage = img;
}

std::string DeviceInfo::GetImageUrl()
{
	return m_imageUrl;
}

void DeviceInfo::SetImageUrl(std::string url)
{
	m_imageUrl = url;
}